//
//  ForgetPassVC.m
//  Ginix
//
//  Created by 冯志强 on 2025/2/14.
//

#import "ForgetPassVC.h"
#import "ForgetSetPassVC.h"
@interface ForgetPassVC ()
@property (nonatomic,strong) UILabel        * nameLabel;
@property (nonatomic,strong) UILabel        * infoLabel;
@property (nonatomic,strong) UITextField    * phoneTextField;
@property (nonatomic,strong) UITextField    * codeTextField;
@property (nonatomic,strong) JKCountDownButton * downBtn;
@property (nonatomic,strong) UIButton       * enterBtn;

@end

@implementation ForgetPassVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"忘记密码";
//    [self.navgationView hideInfo];
    [self setSubViewLayout];
}


-(void)setSubViewLayout{
    
    [self.view addSubview:self.nameLabel];
    [self.view addSubview:self.infoLabel];
    [self.view addSubview:self.phoneTextField];
    [self.view addSubview:self.codeTextField];
    [self.view addSubview:self.enterBtn];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

-(void)enterBtnAction{
    
    if ([self.phoneTextField.text isEqual:@""]) {
        FhudShowStr(@"请输入手机号");
        return;
    }
    if (self.phoneTextField.text.length != 11) {
        FhudShowStr(@"请输入正确手机号");
        return;
    }
    if ([self.codeTextField.text isEqual:@""]) {
        FhudShowStr(@"请输入验证码");
        return;
    }
    NSDictionary * parms = @{@"phone":self.phoneTextField.text ?:@"",@"code":self.codeTextField.text ?:@""};
    @WeakObj(self)
    [SPNetworkingIns POST:PORT(@"auth-center/api/log/resetPhoneVerify") params:parms success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
        @StrongObj(self)
        NSString * sm2Encrypt = K_TimeJson(responseObject, @"msg");
        if (sm2Encrypt.length > 0) {
            ForgetSetPassVC * vc = [[ForgetSetPassVC alloc]init];
            vc.sm2Encrypt = sm2Encrypt;
            vc.phone = self.phoneTextField.text;
            vc.type = self.type;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            FhudShowStr(@"请求错误，请稍后再试");
        }
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
        
   
    
}

-(UILabel *)nameLabel{
    
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(30 * FMScaleX, NavHeight + 12 * FMScaleX, 150 * FMScaleX, 45 * FMScaleX)];
        _nameLabel.textColor = RDRGB(0x714933);
        _nameLabel.font = Bold(32);
        _nameLabel.text = @"重置密码";
    }
    return _nameLabel;
}

-(UILabel *)infoLabel{
    
    if (!_infoLabel) {
        _infoLabel = [[UILabel alloc]initWithFrame:CGRectMake(GETMinX(self.nameLabel), GETMaxY(self.nameLabel) + 5 * FMScaleX, 180 * FMScaleX, 20 * FMScaleX)];
        _infoLabel.font = Regular(14);
        _infoLabel.textColor = RDRGB(0x714933);
        _infoLabel.text = @"中医养生戒指  助您酣然入睡";
    }
    return _infoLabel;
}

-(UITextField *)phoneTextField{
    
    if (!_phoneTextField) {
        _phoneTextField = [[UITextField alloc]initWithFrame:CGRectMake(30 * FMScaleX, GETMaxY(self.infoLabel) + 100 * FMScaleX, WIDTH - 60 * FMScaleX, 40 * FMScaleX)];
        _phoneTextField.font = Regular(16);
        _phoneTextField.textColor = RDRGB(0x714933);
        _phoneTextField.attributedPlaceholder = ({
            NSAttributedString * string = [[NSAttributedString alloc]initWithString:@"请输入手机号" attributes:@{NSForegroundColorAttributeName:RDRGB(0x999999),NSFontAttributeName:Regular(14)}];
            string;
        });
        _phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
        UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, 45 * FMScaleX, _phoneTextField.getWidth, 1)];
        line.backgroundColor = RDRGB(0xCDC1B8);
        [_phoneTextField addSubview:line];
    }
    return _phoneTextField;
}

-(UITextField *)codeTextField{
    
    if (!_codeTextField) {
        _codeTextField = [[UITextField alloc]initWithFrame:CGRectMake(30 * FMScaleX, GETMaxY(self.phoneTextField) + 30 * FMScaleX, WIDTH - 60 * FMScaleX, 40 * FMScaleX)];
        _codeTextField.font = Regular(16);
        _codeTextField.textColor = RDRGB(0x714933);
        _codeTextField.secureTextEntry = YES;
        _codeTextField.attributedPlaceholder = ({
            NSAttributedString * string = [[NSAttributedString alloc]initWithString:@"请输入验证码" attributes:@{NSForegroundColorAttributeName:RDRGB(0x999999),NSFontAttributeName:Regular(14)}];
            string;
        });
        _codeTextField.rightViewMode = UITextFieldViewModeAlways;
        _codeTextField.rightView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 102*FMScaleX, 36*FMScaleX)];
            [view addSubview:self.downBtn];
            view;
        });
        UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, 45 * FMScaleX, _codeTextField.getWidth, 1)];
        line.backgroundColor = RDRGB(0xCDC1B8);
        [_codeTextField addSubview:line];
        _codeTextField.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _codeTextField;
}

-(JKCountDownButton *)downBtn{
    if (!_downBtn) {
        _downBtn = [[JKCountDownButton alloc]initWithFrame:CGRectMake(0, 5 * FMScaleX, 89 * FMScaleX, 26 * FMScaleX)];
        [_downBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_downBtn setTitleColor:RDRGB(0x714933) forState:UIControlStateNormal];
        [_downBtn setCornerRadius:4 * FMScaleX borderWidth:1 borderColor:RDRGB(0xCDC1B8)];
        [_downBtn setBackgroundColor: UIColor.clearColor];
        _downBtn.titleLabel.font = Regular(13);
        @WeakObj(self)
        [_downBtn countDownButtonHandler:^(JKCountDownButton *countDownButton, NSInteger tag) {

            @StrongObj(self)
            if ([self.phoneTextField.text isEqual:@""]) {
                FhudShowStr(@"请输入手机号");
                return;
            }
            if (self.phoneTextField.text.length != 11) {
                FhudShowStr(@"请输入正确手机号");
                return;
            }
            
            NSDictionary * dict = @{@"phone":self.phoneTextField.text ? : @"",@"codeType":@"3"};
            [SPNetworkingIns GET:PORT(@"auth-center/api/log/smsPhone") params:dict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
                [countDownButton startCountDownWithSecond:59];
                countDownButton.userInteractionEnabled = NO;
                FhudShowStr(responseObject[@"msg"])
            } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
                    
            }];
        }];
        [_downBtn countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
            countDownButton.userInteractionEnabled = YES;
            return @"重新获取";
        }];
    }
    return _downBtn;
}

-(UIButton *)enterBtn{
    
    if (!_enterBtn) {
        _enterBtn = [[UIButton alloc]initWithFrame:CGRectMake(30 * FMScaleX, GETMaxY(self.codeTextField) + 50 * FMScaleX, WIDTH - 60 * FMScaleX, 50 * FMScaleX)];
        [_enterBtn setBackgroundImage:[UIImage imageNamed:@"Login_enter_icon"] forState:UIControlStateNormal];
        [_enterBtn setTitle:@"下一步" forState:UIControlStateNormal];
        [_enterBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [_enterBtn addTarget:self action:@selector(enterBtnAction ) forControlEvents:UIControlEventTouchUpInside];
        _enterBtn.titleLabel.font = Bold(20);
    }
    return _enterBtn;
}

@end
